<?php
// esms.php --- Time-stamp: <2008-10-11 19:49:25 root>
// Copyright 2008 Julian Qian
// Author: qianxjun@IBM-L3KWG75
// Version: $Id: esms.php,v 0.0 2008/06/10 16:16:39 qianxjun Exp $
// Keywords: 

include_once "lib/class.esmsservice.php";
include_once "lib/config.php";

define("SHM_ESMS_SIZE", 5000);

// $argv = $_GET;
// var_dump($argv);exit;

if($argv[1]){
	$e = new ESmsService($username,$password,$wsdl,$phonecode,$servecode);
	$e->init();

	switch($argv[1]){
	case "-send":
		$msginfo = array();
		$cmd = explode(",",$argv[2]);
		// phone_number,alarm_type
		switch($cmd[1]){
		case 1:
			$alarm = "alarm1";
			break;
		case 2:
			$alarm = "alarm2";
			break;
		case 3:
			$alarm = "alarm3";
			break;			
		default:
			$alarm = "alarm!!";
			break;
		}
		$msginfo[$cmd[0]] = $alarm;
		// var_dump($msginfo);

		$e->putMsg($msginfo, 0);
		break;
	case "-receive":
		// $info (string) 13800138000:msg0,13800138001:msg1,...
		$info = $e->getMsg();
		// share memory, php MUSCT be compiled with --enable-shmop & --enable-sysvsem
		if( -1 == ($shm_key = ftok("/dev/shm/ehome", "t"))){
			die("ftok error!");
		}
		if( FALSE === ($shm_id = shmop_open($shm_key, "c", 0644, SHM_ESMS_SIZE) )) {
			die("Create shared memory failed!\n");
		}
		shmop_write($shm_id, $info, 0);
		shmop_close($shm_id);
		
		echo $info;
		// if($bc = file_put_contents("out.txt",$info)){
		// 	echo "write $bc bytes into files";
		// }
		// fwrite(STDOUT,$info);
		break;
	case "-clear":
		$e->clear();
		echo "Clear users.";
		break;
	default:
		usage();
	}
}else{
	usage();
}


// $e->logging();
// $e->clear();

function usage(){
	$out = "esms.php, a script to put and send msg via web service\n
Usage:\n
  esms.php -send phonenum1,phonenum2\n
  esms.php -receive
";
	echo $out;
}
?>